// Portable SHA-256 implementation in C
//
//   struct sha256 ctx = {0};
//   unsigned char digest[SHA256LEN];
//   sha256push(&ctx, "Hello, ", 7);
//   sha256push(&ctx, "world!", 6);
//   sha256sum(&ctx, digest);
//
// This is free and unencumbered software released into the public domain.

// Interface
#include <stddef.h>
#include <stdint.h>

#define SHA256LEN 32

struct sha256 {
    uint64_t n;
    uint32_t a, b, c, d, e, f, g, h;
    unsigned char x[64];
};

void sha256push(struct sha256 *, const void *, size_t);
void sha256sum(const struct sha256 *, void *);

// Implementation

static void sha256absorb(struct sha256 *s, const unsigned char *p)
{
    uint32_t w[64];
    uint32_t a=s->a, b=s->b, c=s->c, d=s->d, e=s->e, f=s->f, g=s->g, h=s->h;

    w[ 0] = (uint32_t)p[ 0]<<24|(uint32_t)p[ 1]<<16|(uint32_t)p[ 2]<<8|p[ 3];
    w[ 1] = (uint32_t)p[ 4]<<24|(uint32_t)p[ 5]<<16|(uint32_t)p[ 6]<<8|p[ 7];
    w[ 2] = (uint32_t)p[ 8]<<24|(uint32_t)p[ 9]<<16|(uint32_t)p[10]<<8|p[11];
    w[ 3] = (uint32_t)p[12]<<24|(uint32_t)p[13]<<16|(uint32_t)p[14]<<8|p[15];
    w[ 4] = (uint32_t)p[16]<<24|(uint32_t)p[17]<<16|(uint32_t)p[18]<<8|p[19];
    w[ 5] = (uint32_t)p[20]<<24|(uint32_t)p[21]<<16|(uint32_t)p[22]<<8|p[23];
    w[ 6] = (uint32_t)p[24]<<24|(uint32_t)p[25]<<16|(uint32_t)p[26]<<8|p[27];
    w[ 7] = (uint32_t)p[28]<<24|(uint32_t)p[29]<<16|(uint32_t)p[30]<<8|p[31];
    w[ 8] = (uint32_t)p[32]<<24|(uint32_t)p[33]<<16|(uint32_t)p[34]<<8|p[35];
    w[ 9] = (uint32_t)p[36]<<24|(uint32_t)p[37]<<16|(uint32_t)p[38]<<8|p[39];
    w[10] = (uint32_t)p[40]<<24|(uint32_t)p[41]<<16|(uint32_t)p[42]<<8|p[43];
    w[11] = (uint32_t)p[44]<<24|(uint32_t)p[45]<<16|(uint32_t)p[46]<<8|p[47];
    w[12] = (uint32_t)p[48]<<24|(uint32_t)p[49]<<16|(uint32_t)p[50]<<8|p[51];
    w[13] = (uint32_t)p[52]<<24|(uint32_t)p[53]<<16|(uint32_t)p[54]<<8|p[55];
    w[14] = (uint32_t)p[56]<<24|(uint32_t)p[57]<<16|(uint32_t)p[58]<<8|p[59];
    w[15] = (uint32_t)p[60]<<24|(uint32_t)p[61]<<16|(uint32_t)p[62]<<8|p[63];
    w[16]  = w[ 0] + w[ 9];
    w[16] += ((w[ 1] >>7 | w[ 1]<<25) ^ (w[ 1]>>18 | w[ 1]<<14) ^ w[ 1]>> 3);
    w[16] += ((w[14]>>17 | w[14]<<15) ^ (w[14]>>19 | w[14]<<13) ^ w[14]>>10);
    w[17]  = w[ 1] + w[10];
    w[17] += ((w[ 2] >>7 | w[ 2]<<25) ^ (w[ 2]>>18 | w[ 2]<<14) ^ w[ 2]>> 3);
    w[17] += ((w[15]>>17 | w[15]<<15) ^ (w[15]>>19 | w[15]<<13) ^ w[15]>>10);
    w[18]  = w[ 2] + w[11];
    w[18] += ((w[ 3] >>7 | w[ 3]<<25) ^ (w[ 3]>>18 | w[ 3]<<14) ^ w[ 3]>> 3);
    w[18] += ((w[16]>>17 | w[16]<<15) ^ (w[16]>>19 | w[16]<<13) ^ w[16]>>10);
    w[19]  = w[ 3] + w[12];
    w[19] += ((w[ 4] >>7 | w[ 4]<<25) ^ (w[ 4]>>18 | w[ 4]<<14) ^ w[ 4]>> 3);
    w[19] += ((w[17]>>17 | w[17]<<15) ^ (w[17]>>19 | w[17]<<13) ^ w[17]>>10);
    w[20]  = w[ 4] + w[13];
    w[20] += ((w[ 5] >>7 | w[ 5]<<25) ^ (w[ 5]>>18 | w[ 5]<<14) ^ w[ 5]>> 3);
    w[20] += ((w[18]>>17 | w[18]<<15) ^ (w[18]>>19 | w[18]<<13) ^ w[18]>>10);
    w[21]  = w[ 5] + w[14];
    w[21] += ((w[ 6] >>7 | w[ 6]<<25) ^ (w[ 6]>>18 | w[ 6]<<14) ^ w[ 6]>> 3);
    w[21] += ((w[19]>>17 | w[19]<<15) ^ (w[19]>>19 | w[19]<<13) ^ w[19]>>10);
    w[22]  = w[ 6] + w[15];
    w[22] += ((w[ 7] >>7 | w[ 7]<<25) ^ (w[ 7]>>18 | w[ 7]<<14) ^ w[ 7]>> 3);
    w[22] += ((w[20]>>17 | w[20]<<15) ^ (w[20]>>19 | w[20]<<13) ^ w[20]>>10);
    w[23]  = w[ 7] + w[16];
    w[23] += ((w[ 8] >>7 | w[ 8]<<25) ^ (w[ 8]>>18 | w[ 8]<<14) ^ w[ 8]>> 3);
    w[23] += ((w[21]>>17 | w[21]<<15) ^ (w[21]>>19 | w[21]<<13) ^ w[21]>>10);
    w[24]  = w[ 8] + w[17];
    w[24] += ((w[ 9] >>7 | w[ 9]<<25) ^ (w[ 9]>>18 | w[ 9]<<14) ^ w[ 9]>> 3);
    w[24] += ((w[22]>>17 | w[22]<<15) ^ (w[22]>>19 | w[22]<<13) ^ w[22]>>10);
    w[25]  = w[ 9] + w[18];
    w[25] += ((w[10] >>7 | w[10]<<25) ^ (w[10]>>18 | w[10]<<14) ^ w[10]>> 3);
    w[25] += ((w[23]>>17 | w[23]<<15) ^ (w[23]>>19 | w[23]<<13) ^ w[23]>>10);
    w[26]  = w[10] + w[19];
    w[26] += ((w[11] >>7 | w[11]<<25) ^ (w[11]>>18 | w[11]<<14) ^ w[11]>> 3);
    w[26] += ((w[24]>>17 | w[24]<<15) ^ (w[24]>>19 | w[24]<<13) ^ w[24]>>10);
    w[27]  = w[11] + w[20];
    w[27] += ((w[12] >>7 | w[12]<<25) ^ (w[12]>>18 | w[12]<<14) ^ w[12]>> 3);
    w[27] += ((w[25]>>17 | w[25]<<15) ^ (w[25]>>19 | w[25]<<13) ^ w[25]>>10);
    w[28]  = w[12] + w[21];
    w[28] += ((w[13] >>7 | w[13]<<25) ^ (w[13]>>18 | w[13]<<14) ^ w[13]>> 3);
    w[28] += ((w[26]>>17 | w[26]<<15) ^ (w[26]>>19 | w[26]<<13) ^ w[26]>>10);
    w[29]  = w[13] + w[22];
    w[29] += ((w[14] >>7 | w[14]<<25) ^ (w[14]>>18 | w[14]<<14) ^ w[14]>> 3);
    w[29] += ((w[27]>>17 | w[27]<<15) ^ (w[27]>>19 | w[27]<<13) ^ w[27]>>10);
    w[30]  = w[14] + w[23];
    w[30] += ((w[15] >>7 | w[15]<<25) ^ (w[15]>>18 | w[15]<<14) ^ w[15]>> 3);
    w[30] += ((w[28]>>17 | w[28]<<15) ^ (w[28]>>19 | w[28]<<13) ^ w[28]>>10);
    w[31]  = w[15] + w[24];
    w[31] += ((w[16] >>7 | w[16]<<25) ^ (w[16]>>18 | w[16]<<14) ^ w[16]>> 3);
    w[31] += ((w[29]>>17 | w[29]<<15) ^ (w[29]>>19 | w[29]<<13) ^ w[29]>>10);
    w[32]  = w[16] + w[25];
    w[32] += ((w[17] >>7 | w[17]<<25) ^ (w[17]>>18 | w[17]<<14) ^ w[17]>> 3);
    w[32] += ((w[30]>>17 | w[30]<<15) ^ (w[30]>>19 | w[30]<<13) ^ w[30]>>10);
    w[33]  = w[17] + w[26];
    w[33] += ((w[18] >>7 | w[18]<<25) ^ (w[18]>>18 | w[18]<<14) ^ w[18]>> 3);
    w[33] += ((w[31]>>17 | w[31]<<15) ^ (w[31]>>19 | w[31]<<13) ^ w[31]>>10);
    w[34]  = w[18] + w[27];
    w[34] += ((w[19] >>7 | w[19]<<25) ^ (w[19]>>18 | w[19]<<14) ^ w[19]>> 3);
    w[34] += ((w[32]>>17 | w[32]<<15) ^ (w[32]>>19 | w[32]<<13) ^ w[32]>>10);
    w[35]  = w[19] + w[28];
    w[35] += ((w[20] >>7 | w[20]<<25) ^ (w[20]>>18 | w[20]<<14) ^ w[20]>> 3);
    w[35] += ((w[33]>>17 | w[33]<<15) ^ (w[33]>>19 | w[33]<<13) ^ w[33]>>10);
    w[36]  = w[20] + w[29];
    w[36] += ((w[21] >>7 | w[21]<<25) ^ (w[21]>>18 | w[21]<<14) ^ w[21]>> 3);
    w[36] += ((w[34]>>17 | w[34]<<15) ^ (w[34]>>19 | w[34]<<13) ^ w[34]>>10);
    w[37]  = w[21] + w[30];
    w[37] += ((w[22] >>7 | w[22]<<25) ^ (w[22]>>18 | w[22]<<14) ^ w[22]>> 3);
    w[37] += ((w[35]>>17 | w[35]<<15) ^ (w[35]>>19 | w[35]<<13) ^ w[35]>>10);
    w[38]  = w[22] + w[31];
    w[38] += ((w[23] >>7 | w[23]<<25) ^ (w[23]>>18 | w[23]<<14) ^ w[23]>> 3);
    w[38] += ((w[36]>>17 | w[36]<<15) ^ (w[36]>>19 | w[36]<<13) ^ w[36]>>10);
    w[39]  = w[23] + w[32];
    w[39] += ((w[24] >>7 | w[24]<<25) ^ (w[24]>>18 | w[24]<<14) ^ w[24]>> 3);
    w[39] += ((w[37]>>17 | w[37]<<15) ^ (w[37]>>19 | w[37]<<13) ^ w[37]>>10);
    w[40]  = w[24] + w[33];
    w[40] += ((w[25] >>7 | w[25]<<25) ^ (w[25]>>18 | w[25]<<14) ^ w[25]>> 3);
    w[40] += ((w[38]>>17 | w[38]<<15) ^ (w[38]>>19 | w[38]<<13) ^ w[38]>>10);
    w[41]  = w[25] + w[34];
    w[41] += ((w[26] >>7 | w[26]<<25) ^ (w[26]>>18 | w[26]<<14) ^ w[26]>> 3);
    w[41] += ((w[39]>>17 | w[39]<<15) ^ (w[39]>>19 | w[39]<<13) ^ w[39]>>10);
    w[42]  = w[26] + w[35];
    w[42] += ((w[27] >>7 | w[27]<<25) ^ (w[27]>>18 | w[27]<<14) ^ w[27]>> 3);
    w[42] += ((w[40]>>17 | w[40]<<15) ^ (w[40]>>19 | w[40]<<13) ^ w[40]>>10);
    w[43]  = w[27] + w[36];
    w[43] += ((w[28] >>7 | w[28]<<25) ^ (w[28]>>18 | w[28]<<14) ^ w[28]>> 3);
    w[43] += ((w[41]>>17 | w[41]<<15) ^ (w[41]>>19 | w[41]<<13) ^ w[41]>>10);
    w[44]  = w[28] + w[37];
    w[44] += ((w[29] >>7 | w[29]<<25) ^ (w[29]>>18 | w[29]<<14) ^ w[29]>> 3);
    w[44] += ((w[42]>>17 | w[42]<<15) ^ (w[42]>>19 | w[42]<<13) ^ w[42]>>10);
    w[45]  = w[29] + w[38];
    w[45] += ((w[30] >>7 | w[30]<<25) ^ (w[30]>>18 | w[30]<<14) ^ w[30]>> 3);
    w[45] += ((w[43]>>17 | w[43]<<15) ^ (w[43]>>19 | w[43]<<13) ^ w[43]>>10);
    w[46]  = w[30] + w[39];
    w[46] += ((w[31] >>7 | w[31]<<25) ^ (w[31]>>18 | w[31]<<14) ^ w[31]>> 3);
    w[46] += ((w[44]>>17 | w[44]<<15) ^ (w[44]>>19 | w[44]<<13) ^ w[44]>>10);
    w[47]  = w[31] + w[40];
    w[47] += ((w[32] >>7 | w[32]<<25) ^ (w[32]>>18 | w[32]<<14) ^ w[32]>> 3);
    w[47] += ((w[45]>>17 | w[45]<<15) ^ (w[45]>>19 | w[45]<<13) ^ w[45]>>10);
    w[48]  = w[32] + w[41];
    w[48] += ((w[33] >>7 | w[33]<<25) ^ (w[33]>>18 | w[33]<<14) ^ w[33]>> 3);
    w[48] += ((w[46]>>17 | w[46]<<15) ^ (w[46]>>19 | w[46]<<13) ^ w[46]>>10);
    w[49]  = w[33] + w[42];
    w[49] += ((w[34] >>7 | w[34]<<25) ^ (w[34]>>18 | w[34]<<14) ^ w[34]>> 3);
    w[49] += ((w[47]>>17 | w[47]<<15) ^ (w[47]>>19 | w[47]<<13) ^ w[47]>>10);
    w[50]  = w[34] + w[43];
    w[50] += ((w[35] >>7 | w[35]<<25) ^ (w[35]>>18 | w[35]<<14) ^ w[35]>> 3);
    w[50] += ((w[48]>>17 | w[48]<<15) ^ (w[48]>>19 | w[48]<<13) ^ w[48]>>10);
    w[51]  = w[35] + w[44];
    w[51] += ((w[36] >>7 | w[36]<<25) ^ (w[36]>>18 | w[36]<<14) ^ w[36]>> 3);
    w[51] += ((w[49]>>17 | w[49]<<15) ^ (w[49]>>19 | w[49]<<13) ^ w[49]>>10);
    w[52]  = w[36] + w[45];
    w[52] += ((w[37] >>7 | w[37]<<25) ^ (w[37]>>18 | w[37]<<14) ^ w[37]>> 3);
    w[52] += ((w[50]>>17 | w[50]<<15) ^ (w[50]>>19 | w[50]<<13) ^ w[50]>>10);
    w[53]  = w[37] + w[46];
    w[53] += ((w[38] >>7 | w[38]<<25) ^ (w[38]>>18 | w[38]<<14) ^ w[38]>> 3);
    w[53] += ((w[51]>>17 | w[51]<<15) ^ (w[51]>>19 | w[51]<<13) ^ w[51]>>10);
    w[54]  = w[38] + w[47];
    w[54] += ((w[39] >>7 | w[39]<<25) ^ (w[39]>>18 | w[39]<<14) ^ w[39]>> 3);
    w[54] += ((w[52]>>17 | w[52]<<15) ^ (w[52]>>19 | w[52]<<13) ^ w[52]>>10);
    w[55]  = w[39] + w[48];
    w[55] += ((w[40] >>7 | w[40]<<25) ^ (w[40]>>18 | w[40]<<14) ^ w[40]>> 3);
    w[55] += ((w[53]>>17 | w[53]<<15) ^ (w[53]>>19 | w[53]<<13) ^ w[53]>>10);
    w[56]  = w[40] + w[49];
    w[56] += ((w[41] >>7 | w[41]<<25) ^ (w[41]>>18 | w[41]<<14) ^ w[41]>> 3);
    w[56] += ((w[54]>>17 | w[54]<<15) ^ (w[54]>>19 | w[54]<<13) ^ w[54]>>10);
    w[57]  = w[41] + w[50];
    w[57] += ((w[42] >>7 | w[42]<<25) ^ (w[42]>>18 | w[42]<<14) ^ w[42]>> 3);
    w[57] += ((w[55]>>17 | w[55]<<15) ^ (w[55]>>19 | w[55]<<13) ^ w[55]>>10);
    w[58]  = w[42] + w[51];
    w[58] += ((w[43] >>7 | w[43]<<25) ^ (w[43]>>18 | w[43]<<14) ^ w[43]>> 3);
    w[58] += ((w[56]>>17 | w[56]<<15) ^ (w[56]>>19 | w[56]<<13) ^ w[56]>>10);
    w[59]  = w[43] + w[52];
    w[59] += ((w[44] >>7 | w[44]<<25) ^ (w[44]>>18 | w[44]<<14) ^ w[44]>> 3);
    w[59] += ((w[57]>>17 | w[57]<<15) ^ (w[57]>>19 | w[57]<<13) ^ w[57]>>10);
    w[60]  = w[44] + w[53];
    w[60] += ((w[45] >>7 | w[45]<<25) ^ (w[45]>>18 | w[45]<<14) ^ w[45]>> 3);
    w[60] += ((w[58]>>17 | w[58]<<15) ^ (w[58]>>19 | w[58]<<13) ^ w[58]>>10);
    w[61]  = w[45] + w[54];
    w[61] += ((w[46] >>7 | w[46]<<25) ^ (w[46]>>18 | w[46]<<14) ^ w[46]>> 3);
    w[61] += ((w[59]>>17 | w[59]<<15) ^ (w[59]>>19 | w[59]<<13) ^ w[59]>>10);
    w[62]  = w[46] + w[55];
    w[62] += ((w[47] >>7 | w[47]<<25) ^ (w[47]>>18 | w[47]<<14) ^ w[47]>> 3);
    w[62] += ((w[60]>>17 | w[60]<<15) ^ (w[60]>>19 | w[60]<<13) ^ w[60]>>10);
    w[63]  = w[47] + w[56];
    w[63] += ((w[48] >>7 | w[48]<<25) ^ (w[48]>>18 | w[48]<<14) ^ w[48]>> 3);
    w[63] += ((w[61]>>17 | w[61]<<15) ^ (w[61]>>19 | w[61]<<13) ^ w[61]>>10);

    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[ 0] + 0x428a2f98; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[ 1] + 0x71374491; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[ 2] + 0xb5c0fbcf; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[ 3] + 0xe9b5dba5; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[ 4] + 0x3956c25b; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[ 5] + 0x59f111f1; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[ 6] + 0x923f82a4; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[ 7] + 0xab1c5ed5; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[ 8] + 0xd807aa98; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[ 9] + 0x12835b01; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[10] + 0x243185be; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[11] + 0x550c7dc3; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[12] + 0x72be5d74; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[13] + 0x80deb1fe; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[14] + 0x9bdc06a7; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[15] + 0xc19bf174; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[16] + 0xe49b69c1; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[17] + 0xefbe4786; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[18] + 0x0fc19dc6; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[19] + 0x240ca1cc; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[20] + 0x2de92c6f; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[21] + 0x4a7484aa; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[22] + 0x5cb0a9dc; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[23] + 0x76f988da; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[24] + 0x983e5152; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[25] + 0xa831c66d; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[26] + 0xb00327c8; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[27] + 0xbf597fc7; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[28] + 0xc6e00bf3; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[29] + 0xd5a79147; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[30] + 0x06ca6351; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[31] + 0x14292967; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[32] + 0x27b70a85; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[33] + 0x2e1b2138; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[34] + 0x4d2c6dfc; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[35] + 0x53380d13; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[36] + 0x650a7354; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[37] + 0x766a0abb; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[38] + 0x81c2c92e; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[39] + 0x92722c85; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[40] + 0xa2bfe8a1; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[41] + 0xa81a664b; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[42] + 0xc24b8b70; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[43] + 0xc76c51a3; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[44] + 0xd192e819; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[45] + 0xd6990624; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[46] + 0xf40e3585; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[47] + 0x106aa070; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[48] + 0x19a4c116; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[49] + 0x1e376c08; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[50] + 0x2748774c; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[51] + 0x34b0bcb5; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[52] + 0x391c0cb3; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[53] + 0x4ed8aa4a; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[54] + 0x5b9cca4f; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[55] + 0x682e6ff3; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);
    h += (e>>6 | e<<26)^(e>>11 | e<<21)^(e>>25 | e<< 7); h+=(e&f)^(~e&g);
    h += w[56] + 0x748f82ee; d+=h; h+=(a&b)^(a&c)^(b&c);
    h += (a>>2 | a<<30)^(a>>13 | a<<19)^(a>>22 | a<<10);
    g += (d>>6 | d<<26)^(d>>11 | d<<21)^(d>>25 | d<< 7); g+=(d&e)^(~d&f);
    g += w[57] + 0x78a5636f; c+=g; g+=(h&a)^(h&b)^(a&b);
    g += (h>>2 | h<<30)^(h>>13 | h<<19)^(h>>22 | h<<10);
    f += (c>>6 | c<<26)^(c>>11 | c<<21)^(c>>25 | c<< 7); f+=(c&d)^(~c&e);
    f += w[58] + 0x84c87814; b+=f; f+=(g&h)^(g&a)^(h&a);
    f += (g>>2 | g<<30)^(g>>13 | g<<19)^(g>>22 | g<<10);
    e += (b>>6 | b<<26)^(b>>11 | b<<21)^(b>>25 | b<< 7); e+=(b&c)^(~b&d);
    e += w[59] + 0x8cc70208; a+=e; e+=(f&g)^(f&h)^(g&h);
    e += (f>>2 | f<<30)^(f>>13 | f<<19)^(f>>22 | f<<10);
    d += (a>>6 | a<<26)^(a>>11 | a<<21)^(a>>25 | a<< 7); d+=(a&b)^(~a&c);
    d += w[60] + 0x90befffa; h+=d; d+=(e&f)^(e&g)^(f&g);
    d += (e>>2 | e<<30)^(e>>13 | e<<19)^(e>>22 | e<<10);
    c += (h>>6 | h<<26)^(h>>11 | h<<21)^(h>>25 | h<< 7); c+=(h&a)^(~h&b);
    c += w[61] + 0xa4506ceb; g+=c; c+=(d&e)^(d&f)^(e&f);
    c += (d>>2 | d<<30)^(d>>13 | d<<19)^(d>>22 | d<<10);
    b += (g>>6 | g<<26)^(g>>11 | g<<21)^(g>>25 | g<< 7); b+=(g&h)^(~g&a);
    b += w[62] + 0xbef9a3f7; f+=b; b+=(c&d)^(c&e)^(d&e);
    b += (c>>2 | c<<30)^(c>>13 | c<<19)^(c>>22 | c<<10);
    a += (f>>6 | f<<26)^(f>>11 | f<<21)^(f>>25 | f<< 7); a+=(f&g)^(~f&h);
    a += w[63] + 0xc67178f2; e+=a; a+=(b&c)^(b&d)^(c&d);
    a += (b>>2 | b<<30)^(b>>13 | b<<19)^(b>>22 | b<<10);

    s->a+=a; s->b+=b; s->c+=c; s->d+=d; s->e+=e; s->f+=f; s->g+=g; s->h+=h;
}

void
sha256push(struct sha256 *s, const void *buf, size_t len)
{
    if (!s->n) {
        s->a = 0x6a09e667; s->b = 0xbb67ae85;
        s->c = 0x3c6ef372; s->d = 0xa54ff53a;
        s->e = 0x510e527f; s->f = 0x9b05688c;
        s->g = 0x1f83d9ab; s->h = 0x5be0cd19;
    }

    const unsigned char *p = buf;
    int n = s->n & 63;
    int r = 64 - n;
    s->n += len;
    if (n) {
        int amt = len<(size_t)r ? (int)len : r;
        for (int i = 0; i < amt; i++) {
            s->x[n+i] = p[i];
        }
        p += amt;
        len -= amt;
        if (amt == r) {
            sha256absorb(s, s->x);
        }
    }

    for (; len >= 64; len-=64, p+=64) {
        sha256absorb(s, p);
    }

    for (int rem = len, i = 0; i < rem; i++) {
        s->x[i] = p[i];
    }
}

void
sha256sum(const struct sha256 *s, void *digest)
{
    struct sha256 t;
    if (!s->n) {
        t.n = 0;
        sha256push(&t, 0, 0);
    } else {
        t = *s;
    }

    int n = t.n & 63;
    t.n *= 8;
    t.x[n++] = 0x80;
    if (n > 56) {
        unsigned char buf[64] = {
            [56]=t.n>>56, [57]=t.n>>48, [58]=t.n>>40, [59]=t.n>>32,
            [60]=t.n>>24, [61]=t.n>>16, [62]=t.n>> 8, [63]=t.n>> 0
        };
        for (int i = n; i < 64; i++) {
            t.x[i] = 0;
        }
        sha256absorb(&t, t.x);
        sha256absorb(&t, buf);
    } else {
        for (int i = n; i < 56; i++) {
            t.x[i] = 0;
        }
        t.x[56] = t.n >> 56; t.x[57] = t.n >> 48;
        t.x[58] = t.n >> 40; t.x[59] = t.n >> 32;
        t.x[60] = t.n >> 24; t.x[61] = t.n >> 16;
        t.x[62] = t.n >>  8; t.x[63] = t.n >>  0;
        sha256absorb(&t, t.x);
    }

    unsigned char *p = digest;
    p[ 0] = t.a>>24; p[ 1] = t.a>>16; p[ 2] = t.a>>8; p[ 3] = t.a;
    p[ 4] = t.b>>24; p[ 5] = t.b>>16; p[ 6] = t.b>>8; p[ 7] = t.b;
    p[ 8] = t.c>>24; p[ 9] = t.c>>16; p[10] = t.c>>8; p[11] = t.c;
    p[12] = t.d>>24; p[13] = t.d>>16; p[14] = t.d>>8; p[15] = t.d;
    p[16] = t.e>>24; p[17] = t.e>>16; p[18] = t.e>>8; p[19] = t.e;
    p[20] = t.f>>24; p[21] = t.f>>16; p[22] = t.f>>8; p[23] = t.f;
    p[24] = t.g>>24; p[25] = t.g>>16; p[26] = t.g>>8; p[27] = t.g;
    p[28] = t.h>>24; p[29] = t.h>>16; p[30] = t.h>>8; p[31] = t.h;
}


#if BENCH
// $ cc -DBENCH -O3 -o sha256 sha256.c
// $ time ./sha256 <input
#include <stdio.h>

int
main(void)
{
    #if _WIN32
    int _setmode(int, int);
    _setmode(0, 0x8000);
    _setmode(1, 0x8000);
    #endif

    struct sha256 ctx = {0};
    unsigned char digest[SHA256LEN];
    for (;;) {
        char buf[1<<14];
        int len = fread(buf, 1, sizeof(buf), stdin);
        sha256push(&ctx, buf, len);
        if (len != (int)sizeof(buf)) {
            break;
        }
    }
    sha256sum(&ctx, digest);

    char print[SHA256LEN*2+1];
    for (int i = 0; i < SHA256LEN; i++) {
        print[i*2+0] = "0123456789abcdef"[digest[i]>>4];
        print[i*2+1] = "0123456789abcdef"[digest[i]&15];
    }
    print[2*SHA256LEN] = '\n';
    fwrite(print, sizeof(print), 1, stdout);
    fflush(stdout);
    return ferror(stdin) || ferror(stdout);
}
#endif
